
#include "stdafx.h"
#include "AcquireList.h"
#include "CodeAcquire.h"
#ifndef _BIDCONTROL_
#include "Utility.h"
#else
#include "..\\..\\BidServer\\Utility.h"
#endif

//CAcquireNode
CAcquireNode::CAcquireNode(CCodeAcquire *acq, LPCSTR codeid) :acquirer(acq)
{
	codeId = copyStr(codeid);
}

CAcquireNode::~CAcquireNode()
{
	if (codeId) HeapFree(GetProcessHeap(), 0, (LPVOID)codeId);
	if (acquirer) delete acquirer;
}


//CAcquireList
CAcquireList::CAcquireList() :pendAry(CAcquireNodeArray::CLEAR_METHOD::Delete)
{
}

bool CAcquireList::addPending(CAcquireNode *pNode)
{
	cs_pend.Lock();
	bool bRet = pendAry.add(pNode);
	cs_pend.Unlock();
	return bRet;
}

void CAcquireList::removePending(CAcquireNode *pNode)
{
	cs_pend.Lock();
	for (intptr_t i = pendAry.size() - 1; i >= 0; i--) if (pendAry[i] && pendAry[i] == pNode) pendAry.del(i);
	cs_pend.Unlock();
}

CAcquireNode *CAcquireList::findPending(CCodeAcquire *acquire)
{
	CAcquireNode *result = nullptr;
	cs_pend.Lock();
	for (intptr_t i = 0; i < pendAry.size(); i++) if (pendAry[i] && pendAry[i]->getAcquirer() == acquire)
	{
		result = pendAry[i]; pendAry[i] = nullptr; pendAry.del(i); break;
	}
	cs_pend.Unlock();
	return result;
}
